package com.twu.biblioteca;

public class LibraryManager {
    private final Library library;
    private final UserRegister userRegister;
    private String output = "";
    public User loggedInUser;
    private static LibraryManager libraryManager;

    public LibraryManager(Library library, UserRegister userRegister) {
        this.library = library;
        this.userRegister = userRegister;
    }

    public String welcomeUsers() {
        this.output = Messages.GREETING_MESSAGE;
        return output;
    }

    public String authenticateUser(String username, String password) {
        this.loggedInUser = userRegister.getUserByUsernameAndPassword(username, password);
        if (this.loggedInUser != null) {
            output = Messages.LOGIN_WELCOME_MESSAGE;
        } else {
            output = Messages.UNABLE_TO_LOGIN_MESSAGE;
        }
        return output;
    }

    public String getLoggedInUser() {
        output = Messages.NO_USERS_FOUND;
        if (isLoggedIn()) {
            output = loggedInUser.toString();
        }
        return output;
    }

    public String getOutput() {
        return output;
    }

    public int countBooks() {
        return library.countAvailableBooks();
    }

    public boolean isLoggedIn() {
        return loggedInUser != null;
    }

    public String displayAllBooks() {
        output = library.showAvailableBooks();
        return output;
    }

    public String displayMovies() {
        output = Messages.ALL_MOVIES_HEADER +
                library.displayMovies();
        return output;
    }

    public String reserveBook(String title) {
        if (loggedInUser != null) {
            if(library.reserve(title)){
                output = Messages.ERROR_NOT_LOGGED_IN;
            }
        }
        return output;
    }

    public void printToScreen(String message) {
        System.out.println(message);
    }

    public static LibraryManager getInstance(){
        if(libraryManager == null){
           libraryManager = new LibraryManager(new Library(new Shelf().createBookShelf(), new Shelf().createMovieShelf()), UserRegister.getInstance());
        }
        return libraryManager;
    }

}
